﻿using System;
using System.IO;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Markup;

namespace myWindowsMediaPlayer.Model
{
    class databasePicture
    {
        private List<pictureModel> pictures;

        public databasePicture()
        {
            Regex reg = new Regex(";+");
            myConfigManager conf = new myConfigManager();
            string tmp = conf.PicturePath;
            parseXMLFile();
            if (tmp != null)
                foreach (string path in reg.Split(tmp))
                    reloadXMLPicture(path);
        }

        public void reloadXMLPicture(string path)
        {
            if (path != null && path.Length > 0)
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                FileSystemInfo[] infos = dir.GetFileSystemInfos();
                XDocument doc = XDocument.Load(".\\conf\\picture.xml");
                
                foreach (FileSystemInfo info in infos)
                    if (info is DirectoryInfo)
                        reloadXMLPicture(path + "\\" + info.Name);
                    else if (info is FileInfo)
                    {
                        if (Path.GetExtension(path + "\\" + info.Name).Equals(".png") ||
                            Path.GetExtension(path + "\\" + info.Name).Equals(".jpeg") ||
                            Path.GetExtension(path + "\\" + info.Name).Equals(".gif"))
                        {
                            pictureModel p = new pictureModel
                            {
                                Path = path + "\\" + info.Name,
                                Name = info.Name
                            };
                            if (pictures.Contains(p) == false)
                            {
                                try
                                {
                                    pictures.Add(p);
                                    doc.Root.Add(new XElement("Picture", new XAttribute("path", p.Path),
                                        new XAttribute("name", p.Name), ""));
                                    doc.Save(".\\conf\\picture.xml");
                                }
                                catch (Exception e)
                                {
                                    MessageBox.Show(e.ToString());
                                }
                            }
                            else
                                p = null;
                        }
                    }
              }
          }

        public List<pictureModel> Pictures
        { 
            get {return this.pictures;} 
        }

        private void parseXMLFile()
        {
            try
            {
                XDocument doc = XDocument.Load(".\\conf\\picture.xml");
                this.pictures = (from picture in doc.Root.Descendants("Pictures")
                                 select new pictureModel
                                 {
                                     Path = picture.Attribute("path").Value,
                                     Name = picture.Attribute("name").Value
                                 }).ToList<pictureModel>();
            }
            catch (Exception exc)
            {
                if (!Directory.Exists(".\\conf"))
                    Directory.CreateDirectory(".\\conf");
                if (!File.Exists(".\\conf\\picture.xml"))
                    File.WriteAllText(".\\conf\\picture.xml", @"<pictureFile></pictureFile>");
                System.Windows.Forms.MessageBox.Show(exc.ToString());
                parseXMLFile();
            }   
        }

        public pictureModel addPicture(string path, string name, string size, DateTime date, int id)
        {
            pictureModel newPicture = new pictureModel
            {
                Path = path,
                Name = name,
                Size = size,
                Date = date,
                Id = id
            };

            this.pictures.Add(newPicture);
            return newPicture;
        }

        public void removePicture(pictureModel picture)
        {
            this.pictures.Remove(picture);
        }
    }
}
